//
//  RegisterController.m
//  easystudy
//
//  Created by 陈彦超 on 16/8/30.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "RegisterController.h"
#import "AuthCodeView.h"

@interface RegisterController ()<UIAlertViewDelegate>
{
    __weak IBOutlet AuthCodeView *authView;
}

@end

@implementation RegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.title = @"注册";
    
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image_backBtn"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtn)];
    self.navigationItem.leftBarButtonItem = leftBtn;
    
    
    [authView getAuthcode];
    
    [_phoneNumView drawCornerRadius:5];
    [_phoneNumView drawBorderColor:[UIColor lightGrayColor] Width:1];
    
    [_pswView drawCornerRadius:5];
    [_pswView drawBorderColor:[UIColor lightGrayColor] Width:1];
    
    [_confirmPswView drawCornerRadius:5];
    [_confirmPswView drawBorderColor:[UIColor lightGrayColor] Width:1];
    
    [_codeView drawCornerRadius:5];
    [_codeView drawBorderColor:[UIColor lightGrayColor] Width:1];
    
    
    
}

- (void)backBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

//爱牙服务条款和隐私政策按钮
- (IBAction)privacyBtnClick:(UIButton *)sender
{
    NSLog(@"爱牙服务条款和隐私政策");
}

//提交
- (IBAction)nextStep:(UIButton *)sender
{
    if ([_phoneTF.text length] == 0) {
        [self.view makeToast:@"请输入手机号"];
        return;
    }
    if ([_pswTF.text length] == 0) {
        [self.view makeToast:@"请输入密码"];
        return;
    }
    if ([_confirmPswTF.text length] == 0) {
        [self.view makeToast:@"请再次输入密码"];
        return;
    }
    if ([_codeTF.text length] == 0) {
        [self.view makeToast:@"请输入验证码"];
        return;
    }
    if (![_pswTF.text isEqualToString:_confirmPswTF.text]) {
        [self.view makeToast:@"两次输入的密码不一致"];
        return;
    }
    if (![[_codeTF.text lowercaseString] isEqualToString:[authView.authCodeStr lowercaseString]]) {
        [self.view makeToast:@"输入的验证码有误"];
        return;
    }
    NSDictionary *param = @{@"regphone":_phoneTF.text,@"userpwd":_pswTF.text};
    [WebService registerWithParams:param WithBlock:^(id result, NSError *error) {
        if (!error) {
            if ([result[@"data"][@"code"] boolValue]) {
                [UserEntity sharedUser].UserID = [NSString stringWithFormat:@"%@",result[@"data"][@"custId"]];
                [UserEntity sharedUser].loginSuccess = YES;
                [UserEntity sharedUser].MobilePhone = _phoneTF.text;
                [UserEntity sharedUser].Password = _pswTF.text;
                [[NSNotificationCenter defaultCenter] postNotificationName:NotificationLoginSuccess object:@1];
                [self dismissViewControllerAnimated:YES completion:nil];
            }else{
                [self.view makeToast:@"注册失败"];
            }
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];
    
    
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
        {
            NSLog(@"取消");
        }
            break;
        case 1:
        {
            NSLog(@"好");
        }
            break;
            
        default:
            break;
    }
}

//邮箱界面确认按钮
- (IBAction)confirmBtn:(UIButton *)sender
{
//    if ([_emailTF.text length] == 0) {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"请输入邮箱" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//        [alert show];
//    }
}

//隐藏键盘
- (void)hideKeyboard
{
    [_phoneTF resignFirstResponder];
    [_pswTF resignFirstResponder];
    [_confirmPswTF resignFirstResponder];
    [_codeTF resignFirstResponder];
}

//隐藏键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self hideKeyboard];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
